<?php
class PartAction extends GlobalAction{
	public function index(){
		$this->channel_top();
	}
	public function channel_top(){
		$part = M('part');
    	$part_list = $part->order('part_id asc')->findall();
    	$this->assign('part',$part_list);
    	$this->GlobalName();
		$this->display('channel_top');
    	
	}
	public function channel_list(){
		$part = M('part');
    	if (!empty($_GET['channelid'])){
    		$channelid = "partid = '".$_GET['channelid']."'";
    		$partid = $_GET['channelid'];
    	}else{
    		$first = $part->order('part_id asc')->getField('part_id');
			$channelid = "partid = '".$first."'";
			$partid = $first;
    	}
        $this->assign("partid",$partid);
    	$channel = M('channel');
    	import("@.ORG.Page"); //导入分页类 
        $count = $channel->count();    //计算总数 
        $p = new Page ( $count, 20 ); 
    	$channel = $channel->where($channelid)->limit($p->firstRow.','.$p->listRows)->order('channel_id asc')->findall();
    	$page = $p->show (); 
        $this->assign( "page", $page ); 
    	$this->assign('channel',$channel);
    	$this->GlobalName();
		$this->display('channel_list');
    }
    
    public function channel_add(){
    	$sort = M('channel');
    	$sort = $sort->where("partid = '".$_GET['channelid']."'")->distinct('distinct')->Field('sort,e_sort')->findall();
    	$this->assign('sort',$sort);
    	$this->GlobalName();
		$this->display();
    }
    public function channel_news_get(){
    	$_POST['time'] = time();
    	$channel = M('channel');
    	if (!empty($_FILES['pic']['name'])) {
    		$_POST['pic'] = $this->_upload('0');
    	}
    	$channel->create();
    	if ($channel->add()) {
    		$this->get_alert('新增成功');
    	};
    }
    
    public function channel_edit(){
    	$pid = $_GET['pid'];
    	$channel = M('channel');
    	$sort = $channel->where("partid = '".$_GET['channelid']."'")->distinct('distinct')->Field('sort,e_sort')->findall();
    	$this->assign('sort',$sort);
    	$channel = $channel->where('channel_id = '.$pid)->findall();
    	$this->assign('channel',$channel);
    	$this->GlobalName();
		$this->display();
    }
    public function channel_edit_get(){
    	$channel = M('channel');
    	if (!empty($_FILES['pic']['name'])) {
    		$_POST['pic'] = $this->_upload('0');
    	}else{
    		$_POST['pic'] = $this->cut_pic_s_url($_POST['pic']);
    	}
    	$channel->create();
    	if ($channel->save()) {
    		$this->get_alert('修改成功');
    	};
    }
    
    public function channel_delete(){
    	$channel = M('channel');
    	$del = $_POST['del'];
    	$delete = $del[0];
    	for ($i=1;$i < count($del);$i++){
    		$delete = $delete.','.$del[$i];
    	}
    	if ($channel->delete($delete)) {
    		$this->get_alert('删除成功');
    	};
    }
	public function part_list(){
    	$part = M('part');
    	$part = $part->order('part_id asc')->findall();
    	$this->assign('part',$part);
    	$this->GlobalName();
		$this->display();
    }
    public function part_add(){
    	$this->GlobalName();
		$this->display();
    }
    public function part_news_get(){
    	$_POST['time'] = time();
    	$part = M('part');
    	$part->create();
    	if ($part->add()) {
    		$this->get_alert('新增成功');
    	};
    	$partid = mysql_insert_id();
    	$menu = M('menu');
    	$_POST['url'] = '?a=part&partid='.mysql_insert_id();
    	$menu->create();
    	if ($menu->add()) {
    		$this->get_alert('新增导航成功');
    	};
    	$part->find($partid);
    	$part->meid = mysql_insert_id();
    	$part->save();
    }

    public function part_edit(){
    	$pid = $_GET['pid'];
    	$part = M('part');
    	$part = $part->where('part_id = '.$pid)->findall();
    	$this->assign('part',$part);
    	$this->GlobalName();
		$this->display();
    }
    public function part_edit_get(){
    	$part = M('part');
    	$part->create();
    	if ($part->save()) {
    		$this->get_alert('修改成功');
    	};
    	$menu = M('menu');
    	$menu->find($_POST['meid']);
    	$menu->title = $_POST['title'];
    	$menu->e_title = $_POST['e_title'];
    	if ($menu->save()) {
    		$this->get_alert('导航修改成功');
    	};
    }

    public function part_delete(){
    	$part = M('part');
    	$del = $_POST['del'];
    	$delete = $del[0];
    	for ($i=1;$i < count($del);$i++){
    		$delete = $delete.','.$del[$i];
    	}
    	if ($part->delete($delete)) {
    		$this->get_alert('删除成功');
    	};
    }
    
    public function ajax_show(){
    	$this->ajax_get('part','show_m');
    }
    public function ajax_channel_show(){
    	$this->ajax_get('channel','show_m');
    }
    public function left(){//默认左列表
    	$left = array(
    		array('sort'=>'Part','name'=>'信息管理','value'=>'channel_top'),
    		array('sort'=>'Part','name'=>'栏目管理','value'=>'part_list'),
    		array('sort'=>'Part','name'=>'新增栏目','value'=>'part_add')
    	);
     	$this->assign('user',$_SESSION['User']);
    	$this->assign('sort',$sort);
    	$this->assign('left',$left);
    	$this->display('Index:left');
    }
}
?>